package com.furns.filter;

import com.furns.entity.Manager;
import com.furns.entity.Member;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @Author LuoJiaCheng
 * @ID 2240129126
 * @Version 1.0
 */
public class ManageFilter implements Filter {
    private List<String> excludedUrls;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String strExcludedUrls = filterConfig.getInitParameter("excludedUrls");
        String[] split = strExcludedUrls.split(",");
        excludedUrls = Arrays.asList(split);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest) servletRequest;

        //不包含初始化参数，拦截
        String url = request.getServletPath();
        if(!excludedUrls.contains(url)){
            Manager manager = (Manager) request.getSession().getAttribute("manager");
            if(null==manager){
                request.getRequestDispatcher("/views/manage/manage_login.jsp").forward(servletRequest,servletResponse);
                return;
            }
        }

        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
